package me.tuyou.stream;

import me.tuyou.common.WordStreamGenerator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * 单词统计(流处理)
 */
public class ProcessTest {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<Tuple2<String, Integer>> dataStream = env.addSource(new WordStreamGenerator())
                .process(new ProcessFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void processElement(String s, Context context, Collector<Tuple2<String, Integer>> collector) throws Exception {
                        System.out.println(s);
                        collector.collect(new Tuple2<>(s, 1));
                    }
                })
                .keyBy(0)
                .sum(1);

        dataStream.print();
        env.execute("wordCount");
    }
}
